package cn.dida.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.Pageable;
import cn.dida.model.Region;

/**
 * <p>地区列表 数据交互类</p>
 * @author zhangzhiling
 */
@Repository
public class RegionDao  extends DaoBase{
	
    /**
     * 添加地区
     * @param region 地区实体类
     * @return Integer 受影响的记录行数
     */
    public Integer insert(Region region){

        String sql = "insert into rgn_region(ParentId, Name, RegionType, is_show)"
                + " values (:parentId, :name, :regionType, :isShow)";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("parentId", region.getParentId());
        paramMap.put("name", region.getName());
        paramMap.put("regionType", region.getRegionType());
        paramMap.put("isShow", 1);

        return namedParameterJdbcTemplate.update(sql, paramMap);
    }

    /**
     * 编辑地区
     * @param region 地区实体类
     * @return Integer 受影响的记录行数
     */
    public Integer update(Region region) {
        String sql = "update rgn_region set ParentId=:parentId, Name=:name, RegionType=:regionType where id = :id";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", region.getId());
        paramMap.put("parentId", region.getParentId());
        paramMap.put("name", region.getName());
        paramMap.put("regionType", region.getRegionType());

        return namedParameterJdbcTemplate.update(sql, paramMap);
    }

    /**
     * 操作地区为不显示
     * @param id 地区id
     * @return Integer 受影响的记录行数
     */
    public Integer delete(int id) {
        String sql = "update rgn_region set is_show=0 where id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 地区列表
     * @param parentId 父节点id
     * @param page 分页游标
     * @return Pageable<Region> 地区分页列表
     */
    public Pageable<Region> query(Integer parentId, Integer page) {
        Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from rgn_region rgn where is_show = 1 ");
        StringBuffer sqlQueryResult = new StringBuffer("select rgn.*,rg.name 'parentName' from rgn_region rgn left join rgn_region rg on rgn.ParentId = rg.id where rgn.is_show=1 ");


        StringBuffer sqlWhere = new StringBuffer() ;
        if(parentId != null){
            sqlWhere.append("and rgn.ParentId = ?");
            args = ArrayUtils.add(args, parentId);
        }

        Integer resultTotal = 0;
        // 查询记录总数
        if(sqlWhere != null){
            resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
            // 查询记录
            sqlQueryResult.append(sqlWhere);
        }else{
            resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.toString(), Integer.class, args);

        }
        sqlQueryResult.append(" limit ?,?");

        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<Region> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(Region.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
    }

    /**
     * 获取地区详情
     * @param id 地区id
     * @return Region 地区实体信息
     */
    public Region getPrizeById(int id) {
        String sql = "select * from rgn_region where id = ?";
        try {
            Region region = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Region>(Region.class), id);
            return region;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断地区是否存在
     * @param parentId 地区父节点id
     * @param name 地区名称
     * @return Region 地区实体信息
     */
    public Region isExit(String parentId, String  name){
        String sql = "select * from rgn_region where ParentId = ? and Name like ?";

       List<Region>  list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Region>(Region.class), parentId, name);
        if (null != list && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }
}
